/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.dtseditor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.text.EditorKit;
import jsyntaxpane.DefaultSyntaxKit;
import org.opu.vcforms.template.util.Loadable;

/**
 *
 * @author yura
 */
public class XMLContentEditor extends JPanel implements ContentEditor{

    private JToolBar editToolBar = new JToolBar();
    private JEditorPane fileContent = new JEditorPane();
    private JScrollPane scroll = new JScrollPane(fileContent);
    private HelpPanel helpPanel;

    public XMLContentEditor(HelpPanel helpPanel, byte[] content) {
        super(new BorderLayout());

        this.helpPanel = helpPanel;
        this.helpPanel.disactived();

        initComponents();
        setContentText(content);

        add(editToolBar, BorderLayout.NORTH);
        add(scroll, BorderLayout.CENTER);

    }

    private void initComponents() {
        fileContent.setFont(new Font("Monospaced", 0, 14));
        fileContent.setCaretColor(new Color(153, 204, 255));
        fileContent.setBackground(Color.WHITE);
        fileContent.setContentType("text/xml");

        editToolBar.setRollover(true);
        editToolBar.setFocusable(false);
        editToolBar.setFloatable(false);

        editToolBar.add(Util.initBtn(new JButton(new FormatAction())));

        EditorKit kit = fileContent.getEditorKit();
        if (kit instanceof DefaultSyntaxKit) {
            DefaultSyntaxKit defaultSyntaxKit = (DefaultSyntaxKit) kit;
            defaultSyntaxKit.addToolBarActions(fileContent, editToolBar);
        }

        editToolBar.validate();
    }

    public final void setContentText(byte[] text) {
        try {
            if (text == null || text.length == 0) {
                text = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<template>"
                        + "\n\n</template>").getBytes(Loadable.CHARSET);
            }

            fileContent.read(new InputStreamReader(
                    new ByteArrayInputStream(text), Loadable.CHARSET), "text/xml");
        } catch (IOException ex) {
            Util.niceEx(ex, "When insert text in xml editor: \n", "Insert text in xml editor");
        }

        fileContent.requestFocusInWindow();
    }

    @Override
    public byte[] getContent() {
        try {
            return fileContent.getText().getBytes(Loadable.CHARSET);
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public JPanel asPanel() {
        return this;
    }

    class FormatAction extends AbstractAction {

        public FormatAction() {
            putValue(SMALL_ICON, new ImageIcon
                    (XMLContentEditor.class.getResource("/Format.png")));
            putValue(SHORT_DESCRIPTION, "Format xml text");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                setContentText(Util.formatXml(getContent()));
            } catch (Exception ex) {
                Util.niceEx(ex, "Format xml");
            }
        }
    }

}
